// 转换成折线图格式
import type { ListData } from '@/pages/Device/Locker/List/data';
import moment from 'moment';

type DataType = 'flow_in';

const dataFilter = (data: ListData = []) =>
  data.map((item) => ({
    ...item,
    tick: moment(item.collectDate),
    value: item.collectValue,
  }));

export const timeChartDataGen = (data: ListData, dataTypes: DataType[]) => {
  const filtData = dataFilter(data);
  const rst: Record<string, { x: string; y: number }[]> = {};
  dataTypes.forEach((key) => {
    const obj: Record<number, number> = {};
    filtData.forEach((item) => {
      const tick = `${item.tick.date()}号`;
      obj[tick] = (obj[tick] || 0) + (item.value || 0);
    });
    rst[key] = Object.keys(obj).map((k) => ({ x: k, y: obj[k] }));
  });
  return rst;
};
